package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.GuardrailFaultEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by ding on 2017/8/4.
 */
public interface GuardrailFaultRepository extends CrudRepository<GuardrailFaultEntity, Long> {

    /**
     * @Description: 故障审核DT
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:13
     */
    public List<DTGuardrailFaultDto> getGuardrailFaultCheck4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    public Integer getGuardrailFaultCheckCount4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 获取单位审核人审核状况
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:13
     */
    @Query(value = "select lf.guardrailCheck from GuardrailFaultEntity lf where lf.guardrailFaultId = ?1")
    public Integer getCheck(Integer lampFaultId) throws Exception;

    /**
     * @Description: 主管审核通过
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:18
     */
    @Modifying
    @Query(value = "UPDATE GuardrailFaultEntity  mf SET mf.guardrailMainCheck=?1, mf.guardrailMainCheckerId=?4,mf.guardrailFeedback=?2,mf.guardrailMaintenanceStatus=1,mf.guardrailMainCheckTime=?5 WHERE mf.guardrailFaultId=?3")
    public Integer faultAudit2Success(Integer isPass, String feedback, Integer lampFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 主管审核拒绝
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:20
     *
     */
    @Modifying
    @Query(value = "UPDATE GuardrailFaultEntity  lf SET lf.guardrailMainCheck=?1, lf.guardrailMainCheckerId=?4,lf.guardrailFeedback=?2,lf.guardrailMainCheckTime=?5 WHERE lf.guardrailFaultId=?3")
    public Integer faultAudit2Fail(Integer isPass, String feedback, Integer monitorFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 单位审核人审核
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:22
     */
    @Modifying
    @Query(value = "UPDATE GuardrailFaultEntity  lf SET lf.guardrailCheck= ?1,lf.guardrailCheckerId=?4,lf.guardrailFeedback=?2,lf.guardrailCheckTime=?5 WHERE lf.guardrailFaultId=?3")
    public Integer faultAudit(Integer isPass, String feedback, Integer monitorFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 故障处理DT
     * @Author: jiang-weiron
     * @Date: 2017/8/4 13:23
     */
    public List<DTGuardrailFaultDto> getGuardrailFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer maintenanceStatus) throws Exception ;

    public Integer getGuardrailFaultHandleCount4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer maintenanceStatus) throws Exception;

    /**
     * @Description: 添加施工方案
     * @Author: jiang-weirong
     * @Date: 2017/8/4 13:23
     */
    @Modifying
    @Query(value = "UPDATE GuardrailFaultEntity  lf SET lf.guardrailMaintenanceContent = ?2 WHERE lf.guardrailFaultId =?1")
    public Integer addGuardrailMaintenanceContent(Integer monitorFaultId, String monitorMaintenanceContent) throws Exception;

    /**
     * @Description: 完成施工
     * @Author: jiang-weirong
     * @Date: 2017/8/4 13:24
     */
    @Modifying
    @Query(value = "UPDATE GuardrailFaultEntity  lf SET lf.guardrailMaintenancePhoto = ?1,lf.guardrailMaintainerId=?2,lf.guardrailMaintenanceTime=?3,lf.guardrailMaintenanceExplain=?4,lf.guardrailMaintenanceStatus=2 WHERE lf.guardrailFaultId =?5")
    public Integer finishGuardrailMaintenance(String monitorMaintenancePhoto, Integer monitorMaintainerId, String monitorMaintenanceTime, String monitorMaintenanceExplain, Integer monitorFaultId) throws Exception;

    /**
     * @Description: 获取故障审核list4App
     * @Author: jiang-weirong
     * @Date: 2017/8/4 18:27
     */
    public List<GuardrailFaultDto> getGuardrailFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description: 故障详情
     * @Author: jiang-weirong
     * @Date: 2017/8/14 20:29
     */
    @Query(value = "select new com.hualude.roadsign.dto.GuardrailFaultDto(gf.guardrailCheck,gf.guardrailMainCheck,gf.guardrailFeedback,gf.guardrailMoney," +
            "gf.guardrailFaultLevel,gf.guardrailFaultPhoto,gf.guardrailFaultType,gf.guardrailFaultReason,gf.guardrailFaultCreatorId, gf.guardrailFaultTime) " +
            "from GuardrailFaultEntity gf where gf.guardrailFaultId=?1")
    public GuardrailFaultDto getGuardrailFaultInfo2Fault(Integer guardrailFaultId) throws Exception;


    /**
     * @Description: 护栏故障datatable
     * @Author: pan-jianan
     * @Date: 2017/8/15 12:16
     */
    public Integer getGuardrailReportFaultCount4DT(Integer actorId, Integer userId, Integer orgId, String strSearch) throws Exception;

    public List<DTGuardrailFaultDto> getGuardrailReportFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception;

    /**
     * @Description: 更新故障
     * @Author: pan-jianan
     * @Date: 2017/8/15 12:16
     */
    @Modifying
    @Query(value = "UPDATE GuardrailFaultEntity  gf SET gf.guardrailFaultLevel = ?2,gf.guardrailFaultType = ?3,gf.guardrailFaultPhoto = ?4,gf.guardrailFaultReason = ?5,gf.guardrailMoney =?6 WHERE gf.guardrailFaultId =?1")
    public Integer updateGuardrailFault(Integer guardrailFaultId, Integer guardrailFaultLevel, Integer guardrailFaultType, String guardrailFaultPhoto, String guardrailFaultReason, Double guardrailMoney) throws Exception;

    /**
     * @Description: 删除故障
     * @Author: pan-jianan
     * @Date: 2017/8/15 12:16
     */
    @Modifying
    @Query(value = "delete from GuardrailFaultEntity gf where gf.guardrailFaultId=?1")
    public Integer deleteGuardrailFaultByIds(Integer guardrailFaultId) throws Exception;

    /**
     * @Description: 删除护栏故障
     * @author xu_jiaHui
     * @date 2017/8/18 21:02
     */
    @Modifying
    @Query(value = "delete from GuardrailFaultEntity where guardrailId = ?1")
    public Integer deleteGuardrailFault(Integer guardrailId) throws Exception;

    /**
     * @Description: 是否可上报
     * @Author: pan-jianan
     * @Date: 2017/8/15 17:50
     */
    @Query(value = "select new com.hualude.roadsign.dto.GuardrailFaultDto(gf.guardrailCheck,gf.guardrailMainCheck,gf.guardrailMaintenanceStatus) " +
            "from GuardrailFaultEntity gf where gf.guardrailId=?1  ORDER BY gf.guardrailFaultTime DESC ")
    public List<GuardrailFaultDto> getGuardrailFaultIsReport(Integer guardrailId) throws Exception;


    /**
     * @Description: app获取监控箱故障维修列表
     * @Author: pan-jianan
     * @Date: 2017/8/5 19:27
     */
    public List<GuardrailFaultDto> getGuardrailFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size)throws Exception;

    /**
     * @Description: app 监控箱回执单
     * @Author: pan-jianan
     * @Date: 2017/8/5 19:27
     */
    @Query(value = "select new com.hualude.roadsign.dto.GuardrailFaultDto(mf.guardrailId,mf.guardrailMainCheckTime, mf.guardrailFaultLevel," +
            "mf.guardrailFaultPhoto,mf.guardrailFaultType,mf.guardrailFaultReason,mf.guardrailMaintenancePhoto,mf.guardrailMaintenanceContent,mf.guardrailMaintenanceExplain," +
            "mf.guardrailMaintenanceTime,  m.guardrailProvince, m.guardrailCity," +
            " m.guardrailArea, m.guardrailRoadName,m.guardrailStartRoad,m.guardrailEndRoad,mf.guardrailFeedback)" +
            " from GuardrailEntity m,GuardrailFaultEntity mf " +
            "WHERE m.guardrailId=mf.guardrailId AND mf.guardrailFaultId = ?1")
    public GuardrailFaultDto getGuardrailReceiptInfo4App(Integer faultId) throws Exception;

    /**
     * @Description: 故障list4App
     * @Author: jiang-weirong
     * @Date: 2017/8/9 17:36
     */
    public List<GuardrailFaultDto> getGuardrailFaultReportList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description: 获取上报list
     * @Author: jiang-weirong
     * @Date: 2017/8/19 18:48
     */
    public List<GuardrailFaultDto> getGuardrailFaultList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description: 根据护栏id获取坐标集
     * @Author: pan-jianan
     * @Date: 2017/8/25 14:32
     */
    public GuardrailFaultDto getGuardrailLatLng(Integer guardrailId) throws Exception;



    /**
     * @Description: 护栏坐标以及图片
     * @Author: jiang-weirong
     * @Date: 2017/8/25 19:01
     */
    public List<GuardrailDto> getGuardrailCoordinates4Map(Integer orgId, Integer orgType, Integer actorId) throws Exception;

    /**
     * @Description: 获取推送相关信息
     * @Author: jiang-weirong
     * @Date: 2017/9/11 11:18
     */
    @Query(value = "select new com.hualude.roadsign.dto.GuardrailFaultDto(guardrailId, guardrailOrgId, guardrailMainOrgId, guardrailFaultType) from GuardrailFaultEntity  where guardrailFaultId = ?1")
    public GuardrailFaultDto getInfoByFaultId(Integer lampFaultId) throws Exception;

    @Modifying
    @Query(value = "UPDATE GuardrailFaultEntity  lf SET lf.guardrailCheck= ?1,lf.guardrailCheckerId=?4,lf.guardrailFeedback=?2,lf.guardrailCheckTime=?5,lf.guardrailMainCheck=?1,lf.guardrailMainCheckTime=?5,lf.guardrailMaintenanceStatus=1 WHERE lf.guardrailFaultId=?3")
    public Integer straightFaultAudit(Integer isPass, String feedback, Integer lampFaultId, Integer userId, String dateNowStr);
}
